package com.example.order_system.controller;

import com.example.order_system.model.Order;
import com.example.order_system.util.ResultData;
import com.example.order_system.model.UserInfo;
import com.example.order_system.service.OrderDetailService;
import com.example.order_system.service.OrderService;
import com.example.order_system.util.SessionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @description:
 * @author: chengbaiyi
 * @create: 2022-07-22 20:55
 */
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @Autowired
    private OrderDetailService orderDetailService;

    /**
     * 下单功能
     * @param dids 下单菜品id
     * @param money 金额
     * @return ResultData
     */
    @PostMapping("/addorder")
    public ResultData addOrder(HttpServletRequest req, String dids, BigDecimal money) {
        int state = -1;
        String msg = "未知错误";
        if (StringUtils.hasLength(dids) && StringUtils.hasLength(String.valueOf(money))) {
            UserInfo userInfo = SessionUtil.getSessionUserInfo(req);
            if (userInfo != null) {
                Order order = new Order();
                order.setUid(userInfo.getUid());
                order.setMoney(money);
                orderService.addOrder(order);
                int oid = order.getOid();
                    if (oid > 0) {
                        String[] didStr = dids.split(",");
                        int result = 1;
                        for (String did : didStr) {
                            result = orderDetailService.addDetail(oid, Integer.parseInt(did));
                            if (result <= 0) {
                                // 操作失败
                                break;
                            }
                        }
                        if (result >= 0) {
                            state = 200;
                            msg = "下单成功";
                        } else {
                            msg = "部分订单下单成功";
                        }
                    }
                } else {
                    msg = "订单添加失败";
                }
            } else {
                msg = "未登录";
        }
        return new ResultData(state, null, msg);
    }

    /**
     * 订单表
     * @param req req
     * @param flag 标志
     * @return ResultData
     */
    @GetMapping("/orderlist")
    public ResultData orderList(HttpServletRequest req, String flag) {
        int state = -1;
        List<Order> data = new ArrayList<>();
        String msg = "未知错误！";
        if (StringUtils.hasLength(flag)) {
            // 查询当前用户订单
            UserInfo userInfo = SessionUtil.getSessionUserInfo(req);
            if (userInfo != null) {
                data = orderService.orderList(String.valueOf(userInfo.getUid()));
                state = 200;
            } else {
                msg = "未登录";
            }
        } else {
            data = orderService.orderList(null);
        }
        return new ResultData(state, data, msg);
    }

    /**
     * 修改订单状态
     * @param oid 订单id
     * @param state 状态
     * @return ResultData
     */
    @PostMapping("/uporder")
    public ResultData updateOrder(Integer oid, Integer state) {
        int sta = -1;
        String msg = "未知错误！";
        int data = -1;
        if (oid > 0 && state >= 0) {
            int result = orderService.updateOrder(oid, state);
            if (result > 0) {
                sta = 200;
                msg = "操作成功";
                data = 1;
            }
        } else {
            msg = "参数错误";
        }
        return new ResultData(sta, data, msg);
    }
}
